#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int triangleNumber(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        int res = 0;
        for (int k = 0;k < nums.size();++k)
        {
            int i = 0, j = k - 1;
            while (j - i + 1 >= 2)
            {
                if (nums[i] + nums[j] > nums[k])
                {
                    res += (j - i);
                    --j;
                }
                else
                {
                    ++i;
                }
            }
        }
        return res;
    }
};